# type: ignore
"""init

Revision ID: c3a9a11cc35d
Revises:
Create Date: 2023-09-29 19:08:54.193136+00:00

"""
from __future__ import annotations

import warnings
from typing import TYPE_CHECKING

import sqlalchemy as sa
from alembic import op
from advanced_alchemy.types import GUID, ORA_JSONB, DateTimeUTC
from sqlalchemy import Text  # noqa: F401

if TYPE_CHECKING:
    from collections.abc import Sequence

__all__ = ["downgrade", "upgrade", "schema_upgrades", "schema_downgrades", "data_upgrades", "data_downgrades"]

sa.GUID = GUID
sa.DateTimeUTC = DateTimeUTC
sa.ORA_JSONB = ORA_JSONB

# revision identifiers, used by Alembic.
revision = "c3a9a11cc35d"
down_revision = None
branch_labels = None
depends_on = None


def upgrade() -> None:
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=UserWarning)
        with op.get_context().autocommit_block():
            schema_upgrades()
            data_upgrades()


def downgrade() -> None:
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=UserWarning)
        with op.get_context().autocommit_block():
            data_downgrades()
            schema_downgrades()


def schema_upgrades() -> None:
    """schema upgrade migrations go here."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "tag",
        sa.Column("id", sa.GUID(length=16), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.String(length=255), nullable=True),
        sa.Column("sa_orm_sentinel", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTimeUTC(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTimeUTC(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_tag")),
    )
    op.create_table(
        "team",
        sa.Column("id", sa.GUID(length=16), nullable=False),
        sa.Column("slug", sa.String(length=100), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.String(length=500), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False),
        sa.Column("sa_orm_sentinel", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTimeUTC(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTimeUTC(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_team")),
    )
    with op.batch_alter_table("team", schema=None) as batch_op:
        batch_op.create_index(batch_op.f("ix_team_name"), ["name"], unique=False)
        batch_op.create_index(batch_op.f("ix_team_slug"), ["slug"], unique=True)

    op.create_table(
        "user_account",
        sa.Column("id", sa.GUID(length=16), nullable=False),
        sa.Column("email", sa.String(), nullable=False),
        sa.Column("name", sa.String(), nullable=True),
        sa.Column("hashed_password", sa.String(length=255), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False),
        sa.Column("is_superuser", sa.Boolean(), nullable=False),
        sa.Column("is_verified", sa.Boolean(), nullable=False),
        sa.Column("verified_at", sa.DateTimeUTC(timezone=True), nullable=True),
        sa.Column("sa_orm_sentinel", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTimeUTC(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTimeUTC(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_user_account")),
        sa.UniqueConstraint("email"),
        sa.UniqueConstraint("email", name=op.f("uq_user_account_email")),
    )
    op.create_table(
        "team_invitation",
        sa.Column("id", sa.GUID(length=16), nullable=False),
        sa.Column("team_id", sa.GUID(length=16), nullable=False),
        sa.Column("email", sa.String(), nullable=False),
        sa.Column("role", sa.String(length=50), nullable=False),
        sa.Column("is_accepted", sa.Boolean(), nullable=False),
        sa.Column("invited_by_id", sa.GUID(length=16), nullable=True),
        sa.Column("invited_by_email", sa.String(), nullable=False),
        sa.Column("sa_orm_sentinel", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTimeUTC(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTimeUTC(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(
            ["invited_by_id"],
            ["user_account.id"],
            name=op.f("fk_team_invitation_invited_by_id_user_account"),
            ondelete="set null",
        ),
        sa.ForeignKeyConstraint(
            ["team_id"], ["team.id"], name=op.f("fk_team_invitation_team_id_team"), ondelete="cascade"
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_team_invitation")),
    )
    with op.batch_alter_table("team_invitation", schema=None) as batch_op:
        batch_op.create_index(batch_op.f("ix_team_invitation_email"), ["email"], unique=False)

    op.create_table(
        "team_member",
        sa.Column("id", sa.GUID(length=16), nullable=False),
        sa.Column("user_id", sa.GUID(length=16), nullable=False),
        sa.Column("team_id", sa.GUID(length=16), nullable=False),
        sa.Column("role", sa.String(length=50), nullable=False),
        sa.Column("is_owner", sa.Boolean(), nullable=False),
        sa.Column("sa_orm_sentinel", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTimeUTC(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTimeUTC(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(["team_id"], ["team.id"], name=op.f("fk_team_member_team_id_team"), ondelete="cascade"),
        sa.ForeignKeyConstraint(
            ["user_id"], ["user_account.id"], name=op.f("fk_team_member_user_id_user_account"), ondelete="cascade"
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_team_member")),
        sa.UniqueConstraint("user_id", "team_id", name=op.f("uq_team_member_user_id")),
    )
    with op.batch_alter_table("team_member", schema=None) as batch_op:
        batch_op.create_index(batch_op.f("ix_team_member_role"), ["role"], unique=False)

    op.create_table(
        "team_tag",
        sa.Column("team_id", sa.GUID(length=16), nullable=False),
        sa.Column("tag_id", sa.GUID(length=16), nullable=False),
        sa.ForeignKeyConstraint(["tag_id"], ["tag.id"], name=op.f("fk_team_tag_tag_id_tag"), ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["team_id"], ["team.id"], name=op.f("fk_team_tag_team_id_team"), ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("team_id", "tag_id", name=op.f("pk_team_tag")),
    )
    # ### end Alembic commands ###


def schema_downgrades() -> None:
    """schema downgrade migrations go here."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("team_tag")
    with op.batch_alter_table("team_member", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_team_member_role"))

    op.drop_table("team_member")
    with op.batch_alter_table("team_invitation", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_team_invitation_email"))

    op.drop_table("team_invitation")
    op.drop_table("user_account")
    with op.batch_alter_table("team", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_team_slug"))
        batch_op.drop_index(batch_op.f("ix_team_name"))

    op.drop_table("team")
    op.drop_table("tag")
    # ### end Alembic commands ###


def data_upgrades() -> None:
    """Add any optional data upgrade migrations here!"""


def data_downgrades() -> None:
    """Add any optional data downgrade migrations here!"""
